/*
 * All rights Reserved, Designed By www.goldbeacon.cn
 * @Title:  demo-springboot
 * @Package com.chenzhengrong.demospringboot.manager.impl
 * @author: chenzhengrong
 * @date:   2022/8/17 13:48
 * @version V1.0
 * Copyright © 2022 www.goldbeacon.cn. All Rights Reserved.
 * 注意：本内容仅限于湖南金烽信息科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.chenzhengrong.demospringboot.manager.impl;

import com.chenzhengrong.demospringboot.annotation.Log;
import com.chenzhengrong.demospringboot.domain.dto.RegionDTO;
import com.chenzhengrong.demospringboot.domain.pojo.Region;
import com.chenzhengrong.demospringboot.domain.vo.RegionVO;
import com.chenzhengrong.demospringboot.manager.RegionManager;
import com.chenzhengrong.demospringboot.service.RegionService;
import com.chenzhengrong.demospringboot.util.CustomerBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author chenzhengrong
 * @title: RegionManagerImpl
 * @description: 省市区通用业务处理层
 * @date 2022/8/17 13:48
 */
@Service
@Slf4j
public class RegionManagerImpl implements RegionManager {

    /** 顶级编号 */
    private static final String ROOT_PARENT_CODE = "0";

    @Autowired
    private RegionService regionService;


    /*
    * @description: 读取树数据
    * @param regionVOList 树集合
    * @param parentCode 上级编号
    * @return java.util.List<com.chenzhengrong.demospringboot.domain.dto.RegionDTO>
    * @author chenzhengrong
    * @date 2022/8/18 0:02
    */
    @Override
    public List<RegionDTO> readTree(List<RegionVO> regionVOList, String parentCode) {
        List<RegionDTO> regionDTOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(regionVOList)) {
            for (RegionVO regionVO : regionVOList) {
                RegionDTO regionDTO = new RegionDTO();
                BeanUtils.copyProperties(regionVO, regionDTO);
                regionDTO.setParentCode(parentCode);
                regionDTOS.add(regionDTO);

                List<RegionVO> children = regionVO.getChildren();
                if (!CollectionUtils.isEmpty(children)) {
                    regionDTOS.addAll(readTree(children, regionVO.getCode()));
                }
            }
        }
        return regionDTOS;
    }

    /*
    * @description: 批量插入数据
    * @param list
    * @return void
    * @author chenzhengrong
    * @date 2022/8/18 0:04
    */
    @Override
    @Transactional
    public void batchInsert(List<RegionDTO> list) {
        if (!CollectionUtils.isEmpty(list)) {
            for (RegionDTO regionDTO : list) {
                log.info("regionDTO：{}", regionDTO);
                regionService.insert(regionDTO);
            }
        }
    }

    @Log(desc = "根据地区名称查询地区数据")
    @Override
    public RegionVO query(String name) throws Exception {
        List<RegionDTO> regionDTOS = regionService.findByName(name);
        if (!CollectionUtils.isEmpty(regionDTOS)) {
            RegionDTO regionDTO = regionDTOS.get(0);
            RegionVO regionVO = CustomerBeanUtils.copyBeanProperties(regionDTO, RegionVO::new);
            return builderTree(regionVO, regionDTO.getParentCode());
        }
        throw new Exception("没有匹配到地区信息，请确认地区名是否输入正确！");
    }

    
    /**
     * @description: 生成树结构
     * @param regionVO 区域数据
     * @param parentCode 上级区域代码
     * @return cn.goldbeacon.demo.domain.vo.RegionVO
     * @author zoujie
     * @date 2021/7/21 15:36
     */
    private RegionVO builderTree(RegionVO regionVO, String parentCode) {
        if(!ROOT_PARENT_CODE.equals(parentCode)) {
            RegionDTO parentRegionDTO = regionService.findByCode(parentCode);
            RegionVO parentRegionVO = CustomerBeanUtils.copyBeanProperties(parentRegionDTO, RegionVO::new);
            parentRegionVO.setChildren(Collections.singletonList(regionVO));
            return builderTree(parentRegionVO, parentRegionDTO.getParentCode());
        }
        return regionVO;
    }

    /**
     * @param provinceName
     * @return java.util.List<cn.goldbeacon.demo.domain.vo.RegionVO>
     * @description: 根据省份名称查询该省份下所有城市数据
     * @author zoujie
     * @date 2021/7/23 8:58
     */
    @Override
    public List<RegionVO> queryCityByProvince(String provinceName) {
        return null;
    }
}
